﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace DNA.Mvc.Community
{
    /// <summary>
    /// Presents the Thread object.
    /// </summary>
    [Table("community_threads")]
    public class Thread
    {
        public Thread() { Approved = new DateTime(1800, 1, 1); }

        public Thread(Post post, bool isPinned = false, bool isLocked = false):base()
        {
            Title = post.Title;
            IsPinned = isPinned;
            IsLocked = isLocked;
            LastPosted = post.Posted;
            LastPostUser = post.UserName;
            IsApproved = post.IsApproved;
            Approved = post.Approved;
            Moderator = post.Moderator;
            Author = post.UserName;
            Posted = post.Posted;
            TotalPosts = 1;
            TotalReads = 0;
        }

        [Key]
        public virtual int ID { get; set; }

        /// <summary>
        /// Gets/Sets the approved date.
        /// </summary>
        public virtual DateTime Approved { get; set; }

        [MaxLength(50)]
        public virtual string Author { get; set; }

        [ForeignKey("ForumID")]
        public virtual Forum Forum { get; set; }

        public virtual  int ForumID { get; set; }

        [Required]
        public virtual bool IsApproved { get; set; }

        [Required]
        public virtual bool IsLocked { get; set; }

        [Required]
        public virtual bool IsDeleted { get; set; }

        [Required]
        public virtual bool IsPinned { get; set; }

        [Required]
        public virtual DateTime LastPosted { get; set; }

        public virtual int? LastPostID { get; set; }

        [MaxLength(50)]
        public virtual string LastPostUser { get; set; }

        [MaxLength(1024)]
        public virtual string LinkTo { get; set; }

        [MaxLength(50)]
        public virtual string Moderator { get; set; }

        [Required]
        public virtual bool IsQuestion { get; set; }

        [Required]
        public virtual bool IsResolved { get; set; }

        [Required]
        public virtual DateTime Posted { get; set; }

        public virtual ICollection<Post> Posts { get; set; }

        [Required]
        public virtual int ThreadType { get; set; }

        [Required, MaxLength(255)]
        public virtual string Title { get; set; }

        [Required]
        public virtual int TotalPosts { get; set; }

        [Required]
        public virtual int TotalReads { get; set; }

        [NotMapped]
        public virtual Post ThreadPost
        {
            get
            {
                if (Posts != null)
                    return Posts.FirstOrDefault(p => p.IsThread);
                return null;
            }
        }

        [NotMapped]
        public virtual Post LastPost
        {
            get
            {
                if (LastPostID.HasValue)
                {
                    return Posts.FirstOrDefault(p => p.ID == LastPostID.Value);
                }
                return null;
            }
        }

        [NotMapped]
        public string SubscriptionToken
        {
            get
            {
                return "community-" + this.ForumID.ToString() + "-" + this.ID.ToString();
            }
        }
    }
}
